
// 这里我们引入假数据
var postsData = require("../../../data/posts-data")
// 引入全局
var app = getApp()

Page({
  data:{
    isPlayingMusic:false
  },
  onLoad:function(option){
    // var globalData = app.globalData 
    var postId = option.id;
    this.data.currentPostId = postId

    var postData = postsData.postList[postId];

    this.setData({
      postData:postData
    })

    var postsCollected = wx.getStorageSync('posts_collected');

    if(postsCollected){
      var postCollected = postsCollected[this.data.currentPostId];
      this.setData({
        collected:postCollected
      })
    }else{
      var postsCollected = {};
      postsCollected[postId] = false;
      wx.setStorageSync('posts_collected',postsCollected)
    }
    // 这里用来判断音乐是否播放，并且来判断是不是当前的 id
    if(app.globalData.g_isPlayingMusic && (app.globalData.g_currentMusicPostId===postId)){
      this.setData({
        isPlayingMusic:true
      })
    }
    //将下面代码提取一个方法并且调用
    this.setMusicMonitor()
  },
  setMusicMonitor:function(){
    var that = this
    wx.onBackgroundAudioPlay(function() {
      that.setData({
        isPlayingMusic:true 
      })
      app.globalData.g_isPlayingMusic = true; 
      app.globalData.g_currentMusicPostId = that.data.currentPostId;
    })
    wx.onBackgroundAudioPause(function(){
      that.setData({
        isPlayingMusic:false
      }) 
      app.globalData.g_isPlayingMusic = false;
      app.globalData.g_currentMusicPostId = null;
    })
  },
  onCollectionTap:function(event){
    this.getPostsCollectedAsy()
  },
  getPostsCollectedSyc:function(){
    var postsCollected = wx.getStorageSync('posts_collected');
    var postCollected = postsCollected[this.data.currentPostId];
    postCollected = !postCollected;
    postsCollected[this.data.currentPostId] = postCollected;
    this.showToast(postsCollected,postCollected)
  },
  getPostsCollectedAsy:function(){
    var that = this;
    wx.getStorage({
      key: 'posts_collected',
      success: function(res){
        var postsCollected = res.data;
        var postCollected = postsCollected[that.data.currentPostId];
        postCollected = !postCollected;
        postsCollected[that.data.currentPostId] = postCollected;   
        that.showToast(postsCollected,postCollected)
      },
    })
  },
  showToast:function(postsCollected,postCollected){
    wx.setStorageSync('posts_collected', postsCollected)
    this.setData({
      collected:postCollected
    })
    wx.showToast({
      title:postCollected?"收藏成功":"取消收藏",
      duration:1000,
      icon:"success"
    })
  },
  showModal:function(postsCollected,postCollected){
    var that = this;
    wx.showModal({
      title:"收藏",
      content:postCollected?"收藏该文章？":"取消收藏？",
      cancelText:"取消",
      cancelColor:"red",
      confirmText:"确认",
      confirmColor:"green",
      success:function(res){
        if (res.confirm) {
          console.log('用户点击确定')
          wx.setStorageSync('posts_collected', postsCollected)
          that.setData({
            collected:postCollected
          })
        } 
      }
    })
  },

  onShareTap:function(event){
    var itemList = [
      "分享给微信好友",
      "分享到朋友圈",
      "分享到QQ",
      "分享到微博",
    ]
    wx.showActionSheet({
      itemList:itemList,
      itemColor:"#405f80",
      success:function(res){ //这里我们怎么知道用户到底是点击的是哪一个呢，框架给定参数 res
        wx.showModal({
          title:"用户" + itemList[res.tapIndex],
          content:"用户是否取消" + res.cencel + "现在无法分享"
        })
      }
    })
  },
  onMusicTap:function(){
    console.log(123)
    var isPlayingMusic = this.data.isPlayingMusic;
    var currentPostId = this.data.currentPostId;
    var postData = postsData.postList[currentPostId].music;
    if(isPlayingMusic){
      this.setData({
        isPlayingMusic:false
      })
    }else{
      wx.playBackgroundAudio({
        dataUrl:postData.url,
        title: postData.title,
        coverImgUrl:postData.coverImg,
        success: function(res){
          console.log("success")
        },
        fail: function() {
          // fail
        },
        complete: function() {
          // complete
        }
      })
      this.setData({
        isPlayingMusic:true
      })
    }

  }

})